package com.tg.appcommon.android;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;

import androidx.annotation.NonNull;

import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;

import java.security.MessageDigest;

public class PlayerBlurBgTransformation extends BitmapTransformation {

    private final String mId = "crop_" + PlayerBlurBgTransformation.class.getCanonicalName() + "_r5";
    private int mRadius;
    private float mScale;

    public PlayerBlurBgTransformation(Context context) {
        this(context, 20, 0.125f);
    }

    public PlayerBlurBgTransformation(Context context, int radius, float scale) {
        super();
        this.mScale = scale;
        mRadius = radius;
    }

    @Override
    protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
//        toTransform = BitmapUtil.scaleBitmap(toTransform, mScale);
        int bitmapWidth = toTransform.getWidth();
        int bitmapHeight = toTransform.getHeight();
        float widthRatio = (float) outWidth / bitmapWidth;
        float heightRatio = (float) outHeight / bitmapHeight;
        Matrix matrix = new Matrix();
        matrix.postScale(mScale, mScale * heightRatio / widthRatio); //长和宽放大缩小的比例
        toTransform = Bitmap.createBitmap(toTransform, 0, 0, toTransform.getWidth(), toTransform.getHeight(), matrix, true);
        return BitmapUtil.blurBitmap(TGApplicationBase.getApplicationContext(), toTransform, mRadius);
    }

    public String getId() {
        return mId + "_radius_" + mRadius;
    }

    @Override
    public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {

    }
}
